﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using Koala.Pro.Common;
using Koala.Pro.Models;
using Wpf.Ui.Controls;

namespace Koala.Pro.ViewModels
{
    public partial class SymbolRegularListViewModel : BaseViewModel
    {
        [ObservableProperty]
        private CListItem selectedItem = new CListItem();

        [ObservableProperty]
        private List<CListItem> iconItems = new List<CListItem>();

        [ObservableProperty]
        private List<CListItem> allItems = new List<CListItem>();

        [ObservableProperty]
        private PagingData pagerInfo = new PagingData
        {
            CurrentPageIndex = 1,
            PageSize = 60
        };

        [ObservableProperty]
        private string filter;

        private int SkipCount { get; set; }

        private int MaxResultCount { get; set; }

        public SymbolRegularListViewModel()
        {
            base.Title = "SymbolRegular的图标";
            foreach (KeyValuePair<string, object> item in EnumHelper.GetMemberKeyValue<SymbolRegular>())
            {
                AllItems.Add(new CListItem(item.Key, item.Value.ToString()));
            }
            ResetData();
        }

        public void ResetData()
        {
            GetData();
        }

        [RelayCommand]
        public void GetAll(string iconName)
        {
            if (!iconName.IsNullOrWhiteSpace())
            {
                IconItems = AllItems.Where((CListItem s) => s.Text.Contains(iconName, StringComparison.OrdinalIgnoreCase)).Take(100).ToList();
            }
            else
            {
                ResetData();
            }
        }

        [RelayCommand]
        private void PageUpdated(FunctionEventArgs<int> info)
        {
            PagerInfo.CurrentPageIndex = info.Info;
            GetData();
        }

        protected virtual void ConvertPagingInfo()
        {
            SkipCount = (PagerInfo.CurrentPageIndex - 1) * PagerInfo.PageSize;
            MaxResultCount = PagerInfo.PageSize;
        }

        [RelayCommand]
        public virtual void Search()
        {
            PagerInfo.CurrentPageIndex = 1;
            GetData();
        }

        public virtual void GetData()
        {
            ConvertPagingInfo();
            if (Filter.IsNullOrEmpty())
            {
                IconItems = AllItems.Skip(SkipCount).Take(MaxResultCount).ToList();
                PagerInfo.RecordCount = AllItems.Count;
                return;
            }
            IconItems = AllItems.Where((CListItem s) => s.Text.Contains(Filter, StringComparison.OrdinalIgnoreCase)).Skip(SkipCount).Take(MaxResultCount)
                .ToList();
            PagerInfo.RecordCount = AllItems.Where((CListItem s) => s.Text.Contains(Filter, StringComparison.OrdinalIgnoreCase)).Count();
        }
    }
}
